package com.zzvcom.webapp.action.indexbean;

import java.util.ArrayList;
import java.util.List;

import com.zzvcom.persist.model.admin.FriendLink;
import com.zzvcom.persist.model.admin.ProductQuality;
import com.zzvcom.persist.model.admin.ProductSize;
import com.zzvcom.service.admin.FriendLinkManager;
import com.zzvcom.service.admin.ProductManager;
import com.zzvcom.service.admin.ProductQualityManager;
import com.zzvcom.service.admin.ProductSizeManager;
import com.zzvcom.utils.ServiceUtil;

public class LeftList
{

	public LeftList()
	{
		listInit(this);
	}
	
	private List<Menu> items;
	
	private List<FriendLink> links;
	
	public Menu  getCurrentItem(String quality)
	{
		for(Menu m : items)
		{
			if(m.quality.getQuality().equals(quality))
			{
				return m;
			}
		}
		return null;
	}
	
	private static List<Menu> ITEMS = null;
	
	private static List<FriendLink> LINKS=null;
	
	private static final Object lock=new Object();

	@SuppressWarnings("unchecked")
    public  static void listInit(LeftList list)
	{
         if(ITEMS==null)
         {
        	 synchronized(lock)
        	 {        		 
        		 if(ITEMS!=null)
        		 {
        			 list.items=ITEMS;
        			 list.links=LINKS;
        			 return;// 如果其他的线程已经初始化了就直接退出初始化
        		 }
        		 ITEMS=new ArrayList<Menu>();
        		 ProductQualityManager pqm=(ProductQualityManager)ServiceUtil.getBean("productQualityManager");
        		 ProductSizeManager psm=(ProductSizeManager)ServiceUtil.getBean("productSizeManager");
        		 ProductManager pm=(ProductManager)ServiceUtil.getBean("productManager");
        		 List<ProductQuality> pqs=pqm.queryByHQL("from ProductQuality order by seq");
        		 Menu m=null;
        		 Object count=null;
        		 List<ProductSize> pss=psm.queryByHQL("from ProductSize order by seq");
        		 for(ProductQuality pq :pqs)
        		 {
        			 m=new Menu();
        			 m.setQuality(pq);
        			 for(ProductSize ps:pss)
        			 {
                       count=pm.queryUniqueByHQL("select count(*) from Product p where p.size=? and p.quality=?", ps.getSize(),pq.getQuality());        			   
        			   if((Long)count>0)
                       {
                    	   m.getSizes().add(ps);
                       }
        			 }
        			 if(m.sizes.size()>0)
        			 {
        				 ITEMS.add(m);
        			 }
        			 else
        			 {
        				 pqm.delete(pq);
        			 }
        			 
        		 }
        		 FriendLinkManager flm=(FriendLinkManager)ServiceUtil.getBean("friendLinkManager");
        		 LINKS=flm.getAll();
        	 }
         }
         list.items=ITEMS;
         list.links=LINKS;
	}
	
	public static  void clear()
	{
        synchronized (lock)
        {
    		if(ITEMS!=null)
    		{
            	ITEMS.clear();
        		ITEMS=null;  
    		}
    		if(LINKS!=null)
    		{
    			LINKS.clear();
    			LINKS=null;  
    		}  		
        }
		
	}

	public List<Menu> getItems()
	{
		return items;
	}

	public List<FriendLink> getLinks()
	{
		return links;
	}

	public void setLinks(List<FriendLink> links)
	{
		this.links = links;
	}

	public void setItems(List<Menu> items)
	{
		this.items = items;
	}
}
